package controller.expertInfo.certificateWarn;

import controller.common.BaseController;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.ExpertBaseServices;
import services.expertInfo.ExpertCertificateServices;
import util.DateUtil;
import wt.entity.expertInfo.ExpertBase;
import wt.entity.expertInfo.ExpertCertificate;
import wt.entity.expertInfo.ExpertCertificateExample;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/2.
 */
@Controller


@RequestMapping("expertInfo/certificateWarn")
public class CertificateWarnController extends BaseController {
    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    ExpertBaseServices expertBaseServices;


    @RequiresPermissions("expertInfo:certificateWarn:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {
        Date newDate = new Date();
        Date date = DateUtil.moveDate(newDate, 180);
        ExpertCertificateExample expertCertificateExample = new ExpertCertificateExample();
        expertCertificateExample.setOrderByClause("END_TIME ");
        ExpertCertificateExample.Criteria criteria= expertCertificateExample.createCriteria();
        criteria.andEndTimeLessThan(date);
        if (getLoginAccount().getExpertBase()!=null){
            criteria.andExpertIdEqualTo(getLoginAccount().getExpertBase().getId());
        }



        JSONArray jsonArray=new JSONArray();

        List<ExpertCertificate> expertCertificateList = expertCertificateServices.selectByExample(expertCertificateExample);
        for (ExpertCertificate expertCertificate : expertCertificateList) {

            ExpertBase expertBase = expertBaseServices.selectByPrimaryKey(expertCertificate.getExpertId());
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("name", expertCertificate.getName());
            jsonObject.put("num", expertCertificate.getNum());
            jsonObject.put("startTime", expertCertificate.getStartTime().getTime());
            jsonObject.put("endTime", expertCertificate.getEndTime().getTime());
            jsonObject.put("expertName", expertBase.getName());
            jsonObject.put("idCard", expertBase.getIdCard());
            jsonObject.put("phone", expertBase.getPhone());
            jsonObject.put("type",expertCertificate.getEndTime().getTime()<newDate.getTime()?1:0 );
            jsonArray.add(jsonObject);
        }

        map.put("data", jsonArray);
        return "expertInfo/certificateWarn/index";
    }



    @RequiresPermissions("expertInfo:certificateWarn:default")
    @ResponseBody
    @RequestMapping("welcomeTips")
    public CommonResult welcomeTips() throws Exception {

        CommonResult commonResult=new CommonResult();
        Date date = DateUtil.moveDate( new Date(), 180);
        ExpertCertificateExample expertCertificateExample = new ExpertCertificateExample();
        ExpertCertificateExample.Criteria criteria= expertCertificateExample.createCriteria();
        criteria.andEndTimeLessThan(date);
        if (getLoginAccount().getExpertBase()!=null){
            criteria.andExpertIdEqualTo(getLoginAccount().getExpertBase().getId());
        }
        Map dataMap = new HashMap();
        int count  = expertCertificateServices.countByExample(expertCertificateExample);
        if (count==0){
            return  commonResult.setError();
        }
        dataMap.put("info",count+"个专家证书即将(已经)过期");
        dataMap.put("url","expertInfo/certificateWarn/index");
        commonResult.setData(dataMap);

        return commonResult.setSuccess();
    }


}
